<template>
  <div>
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="getDataList('reset')">
      <el-form-item label="申请人">
        <el-input v-model="queryForm.real_name" maxlength="10" placeholder="申请人" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="queryForm.mobile" type="number" oninput="if(value.length>11)value=value.slice(0,11)" maxlength="11" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getDataList('reset')">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-radio-group v-model="queryForm.audit_status" style="margin-bottom: 30px;" @change="statusChange">
      <el-radio-button :label="null">全部</el-radio-button>
      <el-radio-button :label="0">待审核</el-radio-button>
      <el-radio-button :label="1">已发放</el-radio-button>
      <el-radio-button :label="2">已驳回</el-radio-button>
    </el-radio-group>

    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column align="center" label="序号" width="180">
        <template slot-scope="scope">
          <span>{{(queryForm.page - 1) * queryForm.limit + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="real_name" header-align="center" align="center" label="申请人名称">
      </el-table-column>
      <el-table-column prop="mobile" header-align="center" align="center" label="联系电话">
      </el-table-column>
      <el-table-column prop="donation_amount" header-align="center" align="center" label="申请金额">
      </el-table-column>
      <el-table-column prop="donation_voucher" header-align="center" align="center" label="凭证">
        <template slot-scope="{row}">
          <el-popover placement="left" title trigger="hover">
            <img :src="row.donation_voucher" style="height: 700px;margin-right: 10px;">
            <img slot="reference" @click="$openImg(row.donation_voucher)" :src="row.donation_voucher" style="height: 50px;width: 50px;margin-right: 10px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="audit_status" header-align="center" align="center" label="状态">
        <template slot-scope="{row}">
          <el-tag v-if="row.audit_status===0">待审核</el-tag>
          <el-tag v-if="row.audit_status===1" type="success">已发放</el-tag>
          <el-tag v-if="row.audit_status===2" type="danger">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="issued_quantity" header-align="center" align="center" label="发放数量  （公益积分）">
      </el-table-column>
      <el-table-column prop="create_time" header-align="center" align="center" label="提交时间">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="{row}">
          <el-button type="text" v-if="row.audit_status===0" size="small" @click="openCheck(row)">审核</el-button>
          <el-button type="text" v-if="row.audit_status!==1" size="small" @click="delItem(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="queryForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="queryForm.limit" :total="total" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 审核弹窗 -->
    <el-dialog title="审核" :visible.sync="visible" width="30%" center @close="handleClose">
      <el-form ref="form" :model="form" label-position="top" :rules="rules" label-width="100px">
        <el-form-item label="请输入证书号或支付流水号" prop="serial_number">
          <el-input v-model="form.serial_number" placeholder="请输入" clearable></el-input>
        </el-form-item>
      </el-form>
      <p>注意：已录入的证书号或支付流水号不能重复输入后确认发放</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="danger" :loading="btnLoading" @click="check(2)">审核不通过</el-button>
        <el-button type="primary" :loading="btnLoading" @click="check(1)">确认发放</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import applyWalletApi from '@/api/applyWalletApi.js'
export default {
  data () {
    return {
      queryForm: {
        limit: 10,
        page: 1,
        audit_status: null
      },
      total: 0,
      dataList: [],
      dataListLoading: false,
      visible: false,
      btnLoading: false,
      rules: {
        serial_number: [
          { required: true, message: '请输入请输入证书号或支付流水号', trigger: 'change' }
        ],
      },
      form: {}
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    getDataList (reset) {
      if (reset) this.queryForm.page = 1
      this.dataListLoading = true;
      applyWalletApi.donationPage(this.queryForm).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list;
          this.total = res.data.page.totalCount;
        }
        this.dataListLoading = false;
      })
    },
    reset () {
      this.queryForm = {
        limit: 10,
        page: 1,
        audit_status: null
      }
      this.getDataList()
    },
    statusChange (e) {
      this.queryForm = {
        limit: 10,
        page: 1,
        audit_status: e
      }
      this.getDataList()
    },
    // 关闭审核弹窗
    handleClose () {
      this.form.serial_number = "";
      this.$refs.form.resetFields();
      this.visible = false
    },
    // 打开审核弹窗
    openCheck (item) {
      this.visible = true;
      this.form.id = item.id;
    },
    delItem (item) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        applyWalletApi.donationDel(item.id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getDataList()
          }
        })
      }).catch(() => { });
    },
    openImg (e) {
      window.open(e)
    },
    // 审核按钮
    check (type) {
      this.form.audit_status = type;
      if (type == 1) {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.checkApi(type)
          }
        })
      } else {
        this.checkApi(type)
      }
    },
    // 审核接口
    checkApi (type) {
      applyWalletApi.donationAudit(this.form).then(res => {
        if (res && res.data.code == 200) {
          if (type == 1) {
            this.$message.success('通过成功！')
          } else if (type == 2) {
            this.$message.success('驳回成功！')
          }
          this.visible = false;
          this.getDataList()
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.queryForm.limit = val
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.queryForm.page = val
      this.getDataList()
    },
  }
}
</script>
<style>
.el-form--inline .el-form-item__content {
  width: auto !important;
}
input[type='number'] {
  -moz-appearance: textfield;
}
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
  -webkit-appearance: none;
}
</style>
<style>
</style>